package com.red.wood.module.cdn.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.cdn.dao.CdnIpInfoDao;

@Repository
public class CdnIpInfoDaoImpl implements CdnIpInfoDao {

	private static final Log log = LogFactory.getLog(CdnIpInfoDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(Map<String, Object> param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		list = filter(listPageData(param));
		count = listCount(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	/**
	 * 初级权限：
	 *  依据：机房是本省的，其对应的IP一定是本省的
	 *  判断IP段为本省：根据IP段中的机房ID,找到对应本省的机房、然后从本省的机房找到对应本省的企业，同时满足情况下判定为本省IP
	 * @param param
	 * @return
	 */
	public List<Map<String, Object>> listPageData(Map<String, Object> param){
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(userLevel, "1") || StringUtils.equals(userLevel, "2")) {
			//TODO 子网页面下钻过来
			//sql = getSql(param);
			sql.append("select ip.startIp,ip.endIp,'ipv4' as ipType,ip.cdnName,ip.cdnId,ip.houseAdd,(inet_aton(ip.endIp) - inet_aton(ip.startIp) + 1) as ipNum");
			sql.append(" from t_cdn_ip_statistics ip where 1 = 1");
			sql.append(" and ip.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
		}else {//高级权限
			sql.append("select ip.startIp,ip.endIp,'ipv4' as ipType,ip.cdnName,ip.cdnId,ip.houseAdd,(inet_aton(ip.endIp) - inet_aton(ip.startIp) + 1) as ipNum");
			sql.append(" from t_cdn_ip_statistics ip where 1 = 1");
			sql.append(" and ip.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append("select ip.startIp,ip.endIp,'ipv4' as ipType,ip.cdnName,ip.cdnId,ip.houseAdd,(inet_aton(ip.endIp) - inet_aton(ip.startIp) + 1) as ipNum");
			sql.append(" from t_cdn_ip_statistics ip where 1 = 1");
			sql.append(" and ip.province != '"+userProvince+"'");
			sql.append("and exists (select t1.cdnId from t_cdn_cdnNet_node_house t1 where t1.cdnId = ip.cdnId and t1.HouseProvince = (select provinceid from provinces t where t.province = '"+userProvince+"' limit 1))");
			sql.append(getQueryConf(param));
		}
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}

	public StringBuffer getSql(Map<String, Object> param) {
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		StringBuffer sql = new StringBuffer();
		sql.append("select ip.startIp,ip.endIp,'ipv4' as ipType,(inet_aton(ip.endIp) - inet_aton(ip.startIp) + 1) as ipNum,cdn.cdnName,ip.cdnId,");
		sql.append("       (select house.houseAdd from t_cdn_cdnnet_node_house house where house.cdnId = cdn.cdnId and house.houseId = ip.houseId limit 1 ) as houseAdd");
		sql.append(" from t_cdn_cdndata cdn");
		if(StringUtils.isNotBlank(linkCdnNetId)) {//子网页面下钻过来
			sql.append(" inner join (select ip.cdnId,ip.cdnNetId,ip.houseId,ip.startIp,ip.endIp,ip.province from t_cdn_cdnNet_node_house_ipSeg ip group by ip.cdnId,ip.cdnNetId,ip.houseId,ip.startIp,ip.endIp,ip.province) ip");
		}else {
			sql.append(" inner join (select ip.cdnId,ip.houseId,ip.startIp,ip.endIp,ip.province from t_cdn_cdnNet_node_house_ipSeg ip group by ip.cdnId,ip.houseId,ip.startIp,ip.endIp,ip.province) ip");
		}
		sql.append(" on cdn.cdnId = ip.cdnId");
		//sql.append(" left join (select t.cdnId,t.nodeId,count(1) nodeNum from (select node.cdnId,nodeId from t_cdn_cdnNetInfo_node node group by node.cdnId,node.nodeId) t group by t.cdnId,t.nodeId) node");
		//sql.append(" on ip.cdnId = node.cdnId and ip.nodeId = node.nodeId");
		sql.append(" where 1 = 1");
		sql.append(" and cdn.province ='"+userProvince+"'");
		return sql;
	}
	
	public StringBuffer getSqlCount(Map<String, Object> param) {
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		StringBuffer sql = new StringBuffer();
		sql.append("select ip.startIp,ip.endIp,'ipv4' as ipType,(inet_aton(ip.endIp) - inet_aton(ip.startIp) + 1) as ipNum,cdn.cdnName,ip.cdnId");
		sql.append(" from t_cdn_cdndata cdn");
		if(StringUtils.isNotBlank(linkCdnNetId)) {//子网页面下钻过来
			sql.append(" inner join (select ip.cdnId,ip.cdnNetId,ip.houseId,ip.startIp,ip.endIp,ip.province from t_cdn_cdnNet_node_house_ipSeg ip group by ip.cdnId,ip.cdnNetId,ip.houseId,ip.startIp,ip.endIp,ip.province) ip");
		}else {
			sql.append(" inner join (select ip.cdnId,ip.houseId,ip.startIp,ip.endIp from t_cdn_cdnNet_node_house_ipSeg ip group by ip.cdnId,ip.houseId,ip.startIp,ip.endIp) ip");
		}
		sql.append(" on cdn.cdnId = ip.cdnId");
		//sql.append(" left join (select t.cdnId,t.nodeId,count(1) nodeNum from (select node.cdnId,nodeId from t_cdn_cdnNetInfo_node node group by node.cdnId,node.nodeId) t group by t.cdnId,t.nodeId) node");
		//sql.append(" on ip.cdnId = node.cdnId and ip.nodeId = node.nodeId");
		sql.append(" where 1 = 1");
		return sql;
	}
	
	public String listCount(Map<String, Object> param) {
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		StringBuffer sql = new StringBuffer();
		
		if(StringUtils.equals(userLevel, "3")) {
			sql.append("select count(*) count from (");
			sql.append("select ip.startIp,ip.endIp,'ipv4' as ipType,ip.cdnName,ip.cdnId,ip.houseAdd,(inet_aton(ip.endIp) - inet_aton(ip.startIp) + 1) as ipNum");
			sql.append(" from t_cdn_ip_statistics ip where 1 = 1");
			sql.append(" and ip.province = '"+userProvince+"'");
			sql.append(getQueryConf(param));
			sql.append(" union all ");
			sql.append("select ip.startIp,ip.endIp,'ipv4' as ipType,ip.cdnName,ip.cdnId,ip.houseAdd,(inet_aton(ip.endIp) - inet_aton(ip.startIp) + 1) as ipNum");
			sql.append(" from t_cdn_ip_statistics ip where 1 = 1");
			sql.append(" and ip.province != '"+userProvince+"'");
			sql.append("and exists (select t1.cdnId from t_cdn_cdnNet_node_house t1 where t1.cdnId = ip.cdnId and t1.HouseProvince = (select provinceid from provinces t where t.province = '"+userProvince+"' limit 1))");
			sql.append(getQueryConf(param));
			sql.append(") a");
		}else {
			sql.append("select count(*) count from t_cdn_ip_statistics ip where 1 = 1");
			//sql.append(getSqlCount(param));
			sql.append(getQueryConf(param));
		}
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getQueryConf(Map<String,Object> param) {
		String userProvince = CommonUtils.getLoginUserProvince(CommonUtils.valueOf(param.get("loginUser")));
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		String linkHouseId = CommonUtils.valueOf(param.get("linkHouseId"));
		String reportProvinceCity = CommonUtils.valueOf(param.get("reportProvinceCity"));
		String cdnName = CommonUtils.valueOf(param.get("cdnName"));
		String reportTimeStart = CommonUtils.valueOf(param.get("reportTimeStart"));
		String reportTimeEnd = CommonUtils.valueOf(param.get("reportTimeEnd"));
		String cdnId = CommonUtils.valueOf(param.get("cdnId"));
		String userLevel = CommonUtils.valueOf(param.get("userLevel"));
		StringBuffer queryConf = new StringBuffer();
		
		/*if(!StringUtils.equals(userLevel, "3")) {
			if(StringUtils.isNotBlank(userProvince)) {
				queryConf.append(" and cdn.province ='"+userProvince+"'");
				if(StringUtils.equals(userLevel, "1")) {//初级权限
					queryConf.append(" and ip.province = '"+userProvince+"'");
				}else if(StringUtils.equals(userLevel, "2")) {//中级权限
					//本省企业下资源不受限制
				}
			}
		}*/
		
		if(StringUtils.isNotBlank(linkHouseId)) {
			queryConf.append(" and ip.houseId='"+linkHouseId+"'");
		}
		if(StringUtils.isNotBlank(linkCdnNetId)) {
			queryConf.append(" and ip.cdnNetId='"+linkCdnNetId+"'");
		}
		if(StringUtils.isNotBlank(cdnId)) {
			queryConf.append(" and ip.cdnId='"+cdnId+"'");
		}
		if(StringUtils.isNotBlank(cdnName)) {
			queryConf.append(" and ip.cdnName like '%"+cdnName+"%'");
		}
		return queryConf;
	}
	
	public List<Map<String, Object>> filter(List<Map<String, Object>> list){
		if(list != null && list.size() > 0) {
			for(Map<String,Object> map : list) {
				map.put("nodeNum",CommonUtils.filteNull(map.get("nodeNum")));
			}
		}
		return list;
	}
}
